<template>
    <div>

        <el-page-header style="background-color: #0095d7;color: #ffffff;line-height: 60px"
                        @back="goReturn" content="轮播图管理"></el-page-header>

        <!--分割线-->
        <el-divider></el-divider>

        <el-card>
            <el-form :model="show" ref="ruleForm">

                <el-form-item label="图片" label-width="80px">
                    <el-upload
                            action="http://localhost:18083/show/upload"
                            name="picFile"
                            :limit="5"
                            list-type="picture-card"
                            :on-preview="handlePictureCardPreview"
                            :on-success="handleSuccess"
                            :on-remove="handleRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img  width="100%" :src="dialogImageUrl" alt="" />
                    </el-dialog>
                </el-form-item>

                <el-form-item>
                    <el-button v-on:click="add('ruleForm')">增加</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                show:{
                    img:'',
                }
            }
        },
        methods:{

            add(){
              console.log(this.show.img)
                let url = 'http://localhost:18083/show/add-new'
                let data = 'img='+this.show.img;
              this.axios.post(url,data).then((response) =>{
                  let message = response.data.message;
                  if (message == null) {
                      this.$message({
                          message: '添加咨询成功',
                          type: 'success'
                      });
                      //提交成功后返回到用户管理页面
                      this.$router.push({path: '/admin/manage/show'});//路由刷新

                  }else{
                      console.log(message);
                      this.$message.error(message)
                  }
              })
            },




            //删除文件
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //当点击删除图片时方法会执行
                //file代表要删除的文件
                //file.response代表文件上传成功时 服务器响应的数据(文件名)
                console.log("文件名="+file.response);
                //http://localhost:8080/remove?name=xxx.jpg
                let url = "http://localhost:18083/show/rul/remove"
                let data = 'name=' + file.response;
                this.axios.post(url,data).then((response) => {
                    console.log("服务器图片已经删除")
                })
            },



            //得到图片的文件名
            handleSuccess(res, file) {//res代表文件名称

                this.show.img = res;
                this.img = file;

                console.log(file);
                console.log(res);
            },
            //上传文件
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            goReturn(){
                //返回上一页面
                history.back();
            }
        }

    }
</script>

<style scoped>

</style>